package com.company.project.service;

import com.company.project.dao.UserMapper;
import com.company.project.model.User;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * @Author: 陶鹏飞
 * @Description:
 * @Date: 2018/12/28-19:49
 * @email: taopengfei1223@foxmail.com
 **/
@Component
public class UserHandler {

    @Resource
    private UserMapper userMapper;

    public Mono<ServerResponse> sayGood(ServerRequest request){
        return ok().contentType(MediaType.TEXT_PLAIN)
                .body(BodyInserters.fromObject("hello,city"));
    }

    public Mono<ServerResponse> getDate(ServerRequest serverRequest) {
        return ok().contentType(MediaType.TEXT_PLAIN)
                .body(Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())), String.class);
    }

    public Mono<ServerResponse> getAlll(ServerRequest serverRequest){
        return ok().contentType(MediaType.TEXT_PLAIN)
                .body((Publisher<User>) userMapper.selectByIds("1"),User.class);
    }
}
